﻿using NLog;
using System.ComponentModel;

namespace Ids4.Api.Application.Loging
{
    /// <summary>
    /// LogType日志类型
    /// </summary>
    public enum LogType
    {
        /// <summary>
        /// Web
        /// </summary>
        [Description("Web")]
        Web,
        /// <summary>
        /// ApiRequest
        /// </summary>
        [Description("Api接口")]
        ApiRequest,
        /// <summary>
        /// DataBase
        /// </summary>
        [Description("数据库")]
        DataBase,
        /// <summary>
        /// Middleware
        /// </summary>
        [Description("中间件")]
        Middleware,
        /// <summary>
        /// Other
        /// </summary>
        [Description("其他")]
        Other
    }

    /// <summary>
    /// NLogUtil
    /// </summary>
    public static class NLogUtil
    {
        private static readonly NLog.Logger FileLogger = LogManager.GetLogger("logfile");

        /// <summary>
        /// 写日志到文件
        /// </summary>
        /// <param name="logLevel">日志等级</param>
        /// <param name="logType">日志类型</param>
        /// <param name="logTitle">标题（255字符）</param>
        /// <param name="message">信息</param>
        /// <param name="exception">异常</param>
        public static void WriteFileLog(NLog.LogLevel logLevel, LogType logType, string logTitle, string message, Exception exception = null)
        {
            LogEventInfo theEvent = new LogEventInfo(logLevel, FileLogger.Name, message);
            theEvent.Properties["LogType"] = logType.ToString();
            theEvent.Properties["LogTitle"] = logTitle;
            theEvent.Exception = exception;
            FileLogger.Log(theEvent);
        }
    }
}
